<?php

/**
 * This is the model class for table "product_attribute".
 *
 * The followings are the available columns in table 'product_attribute':
 * @property string $id
 * @property string $product_type
 * @property string $game_id
 * @property string $default_value
 * @property string $input_type
 * @property string $value_list
 * @property string $order_sort
 * @property integer $status
 */
class ProductAttribute extends CActiveRecord {

    //属性类型
    public $input_type_array = array(
        'TEXT' => '文本框',
        'DROPDOWN' => '下拉框',
        'NUMS' => '数量集'
    );
    //状态
    public $status_array = array(0 => '停用', 1 => '启用');

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return ProductAttribute the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'product_attribute';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, game_id, product_type', 'required'),
            array('status', 'numerical', 'integerOnly' => true),
            array('product_type', 'length', 'max' => 20),
            array('game_id, order_sort', 'length', 'max' => 11),
            array('default_value, input_type, value_list, name', 'length', 'max' => 255),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, product_type, game_id, default_value, input_type, value_list, order_sort, status, name', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'product_type' => '产品类型',
            'game_id' => '游戏',
            'default_value' => '默认值',
            'input_type' => '属性类型',
            'value_list' => '可选值',
            'order_sort' => '排序',
            'status' => '状态',
            'name' => '属性名称'
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('product_type', $this->product_type);
        $criteria->compare('game_id', $this->game_id);
        $criteria->compare('status', $this->status);
        $pageSize = 30;
        if($this->game_id && $this->product_type)
            $pageSize = 10000;

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                    'pagination' => array(
                        'pageSize' => $pageSize
                    ),
                ));
    }

    /**
     * 属性表单元素生成
     * @param int $game 游戏ID
     * @param str $type 产品模型
     * @param int $pid 产品ID
     * @return string 
     */
    public function getAttrInput($game, $type, $pid = 0) {
        $game_data = ProductCategory::model()->findByPk($game);
        $cr = new CDbCriteria;
        $cr->compare('game_id', $game_data->parentid);
        $cr->compare('product_type', $type);
        $cr->compare('status', 1);
        $cr->order = 'order_sort DESC';
        $data = ProductAttribute::model()->findAll($cr);
        $inputStr = '';
        foreach ($data as $v) {
            $inputStr .= '<div class="row">';
            $inputStr .= CHtml::label($v->name, '');
            if ($pid > 0) {
                $attr_value = ProductAttributeValue::model()->findByAttributes(array(
                    'product_id' => $pid,
                    'attribute_id' => $v->id,
                        ));
                if ($attr_value) {
                    switch ($v->input_type) {
                        case 'NUMS'://数量集属性，内容为经过序列化的数组
                            $v->default_value = unserialize($attr_value->attribute_value);
                            break;
                        default :
                            $v->default_value = $attr_value->attribute_value;
                            break;
                    }
                }
            }
            switch ($v->input_type) {
                case 'NUMS'://数量集属性，内容为经过序列化的数组
                    if (!empty($v->default_value)) {
                        foreach ($v->default_value['num'] as $key => $v2) {
                            $inputStr .= "<div style='margin-left:100px;margin-bottom:5px;'>
                        <label style='width:50px'>数量</label><input name='attr[$v->id][num][]' type='text' value='" . $v->default_value['num'][$key] . "' /> 
                        <label style='width:50px'>单位</label><input name='attr[$v->id][unit][]' type='text' value='" . $v->default_value['unit'][$key] . "' /> 
                        <label style='width:50px'>折扣</label><input name='attr[$v->id][discount][]' type='text' value='" . $v->default_value['discount'][$key] . "' /> 
                        <input type='button' value='+' onclick='add(this)' />
                        <div class='clear'></div>
                        </div>
                        ";
                        }
                    } else {
                        $inputStr .= "<div style='margin-left:100px;margin-bottom:5px;'>
                        <label style='width:50px'>数量</label><input name='attr[$v->id][num][]' type='text' /> 
                        <label style='width:50px'>单位</label><input name='attr[$v->id][unit][]' type='text' /> 
                        <label style='width:50px'>折扣</label><input name='attr[$v->id][discount][]' type='text' /> 
                        <input type='button' value='+' onclick='add(this)' />
                        <div class='clear'></div>
                        </div>
                        ";
                    }
                    

                    break;
                case 'DROPDOWN':
                    $drop_list = explode("\r\n", $v->value_list);
                    
                    $inputStr .= CHtml::dropDownList("attr[$v->id]", $v->default_value, array('' => '') + array_combine($drop_list, $drop_list));
                    break;
                default ://其它属性
                    $inputStr .= CHtml::textField("attr[$v->id]", $v->default_value);
                    break;
            }
            $inputStr .= '</div>';
        }
        return $inputStr;
    }

    /**
     * 显示产品的属性值
     * @param type $pid
     * @return type
     */
    function showAttValue($pid) {
        $product = ProductBase::model()->findByPk($pid);
        $game_info = ProductCategory::model()->findByPk($product->p_game);
        //属性
        $attrs = $this->findAllByAttributes(array('product_type' => $product->p_type, 'game_id' => $game_info->parentid));
        $str = "";
        foreach ($attrs as $attr) {
            $str .= "<br/><B>" . $attr->name . ":</B> ";
            //属性值
            $attrValue = ProductAttributeValue::model()->findByAttributes(array(
                'product_id' => $pid,
                'attribute_id' => $attr->id,
                    ));
            switch ($attr->input_type) {
                case 'NUMS':
                    $value = !empty($attrValue->attribute_value) ? unserialize($attrValue->attribute_value) : "";
                    $thead = "";
                    $tbody = "";
                    foreach ($value as $key => $vv) {
                        $thead .= "<tr><th>$key</th>";
                        $tbody = "";
                        foreach ($vv as $vvv) {
                            $tbody .= "<td>$vvv</td>";
                        }
                        $thead .= "$tbody</tr>";
                    }
                    $str .= "<table width='100%'>$thead</table>";
                    break;
                default :
                    $str .= $attrValue->attribute_value;
                    break;
            }
        }
        return $str;
    }

}